<?
class Contact_orgModel extends Master_Model {
/**
 * MODULE NAME   : contact_orgmodel.php
 *
 * DESCRIPTION   : Contact_org model controller
 *
 * MODIFICATION HISTORY
 *   V1.0   2007-04-22 02:31 AM   - Dennis T. Kaplan     - Created
 *
 * @package             contact_org
 * @subpackage          Contact_org model component Class
 * @author              Dennis T. Kaplan
 * @copyright           Copyright &copy; 2006-2007 Dennis T. Kaplan
 * @since               Version 1.0
 * @filesource
 */

   function Contact_orgModel()
   {
      parent::Model();
      //$this->obj =& get_instance();
      $this->mdb->initdb();
   }

   // //////////////////////////////////////////////////////////////////////////
   // Function: findAll()
   //
   // Description: Retrieves and returns data listing from the database
   //
   // //////////////////////////////////////////////////////////////////////////
   function findAll($start = NULL, $count = NULL) {
      return $this->find(NULL, $start, $count);
   }

//   function findById($key_value) {
//      return $this->find(array('id' => '$key_value'));
//   }

   function findByFilter($filter_rules, $start = NULL, $count = NULL) {
      return $this->find($filter_rules, $start, $count);
   }

   function find($filters = NULL, $start = NULL, $count = NULL) {

      $results = array();

      // Load the database library
      $this->load->database();

      // ///////////////////////////////////////////////////////////////////////
      // Make a note of the current table record count
      // ///////////////////////////////////////////////////////////////////////
      $this->table_record_count = $this->db->count_all( 'contact_org' );


      // Filter could be an array or filter values or an SQL string.
      $where_clause = '';
      if ($filters) {
         if ( is_string($filters) ) {
            $where_clause = $filters;
         }
         elseif ( is_array($filters) ) {
            // Build your filter rules
            if ( count($filters) > 0 ) {
               foreach ($filters as $field => $value) {
                  $filter_list[] = " $field = '$value' ";
               }
               $where_clause = ' WHERE ' . join(' AND ', $filter_list );
            }
         }

      }

      $limit_clause = '';
      if ($start) {
         if ($count) {
            $limit_clause = " $start, $count ";
         }
         else {
            $limit_clause = " $start ";
         }
      }

      // Build up the SQL query string and run the query
      $sql = 'SELECT * FROM contact_org ' . $where_clause . $limit_clause;

      $query = $this->db->query($sql);

      if ($query->num_rows() > 0) {
         // ////////////////////////////////////////////////////////////////////
         // NOTE: At this stage you could return the entire result set, like:
         // NOTE: ...return $query->result_array();
         // NOTE: ...The generated code loops through the result set to provide
         // NOTE: ...the oppurtunity to provide further customisations on the
         // NOTE: ...code (especially if you are generating in verbose mode).
         // ////////////////////////////////////////////////////////////////////

         foreach ($query->result_array() as $row)      // Go through the result set
         {
            // Build up a list for each column from the database and place it in
            // ...the result set

			$query_results['id']		 = $row['id'];
			$query_results['people_id']		 = $row['people_id'];
			$query_results['Name']		 = $row['Name'];
			$query_results['Org_Type']		 = $row['Org_Type'];
			$query_results['Street']		 = $row['Street'];
			$query_results['Postcode']		 = $row['Postcode'];
			$query_results['Country']		 = $row['Country'];
			$query_results['Phone']		 = $row['Phone'];
			$query_results['FAX']		 = $row['FAX'];
			$query_results['Website']		 = $row['Website'];
			$query_results['Notes']		 = $row['Notes'];
			$query_results['first']		 = $row['first'];
			$query_results['last']		 = $row['last'];

			$results[]		 = $query_results;


         }

      }

      return $results;

   }


   // TODO: this won't be possible if there is no primary key for the table.
   function retrieve_by_pkey($idField) {

      $results = array();

      // Load  the db library
      $this->load->database();

      $query = $this->db->query("SELECT * FROM contact_org WHERE id = '$idField' LIMIT 1");

      if ($query->num_rows() > 0) {
         $row = $query->row_array();

		$query_results['id']		 = $row['id'];
		$query_results['people_id']		 = $row['people_id'];
		$query_results['Name']		 = $row['Name'];
		$query_results['Org_Type']		 = $row['Org_Type'];
		$query_results['Street']		 = $row['Street'];
		$query_results['Postcode']		 = $row['Postcode'];
		$query_results['Country']		 = $row['Country'];
		$query_results['Phone']		 = $row['Phone'];
		$query_results['FAX']		 = $row['FAX'];
		$query_results['Website']		 = $row['Website'];
		$query_results['Notes']		 = $row['Notes'];
		$query_results['first']		 = $row['first'];
		$query_results['last']		 = $row['last'];

		$results		 = $query_results;


      }
      else {
         $results = false;
      }

      return $results;
   }


   function add( $data ) {

      // Load the database library
      $this->load->database();

      // Build up the SQL query string
      $sql = $this->db->insert_string('contact_org', $data);

      $query = $this->db->query($sql);

      return $this->db->insert_id();
   }

   function modify($keyvalue, $data) {


      // Load the database library
      $this->load->database();

      // Build up the SQL query string
      $where = "id = $keyvalue";
      $sql = $this->db->update_string('contact_org', $data, $where);

      $query = $this->db->query($sql);

   }

   function delete_by_pkey($idField)
   {
       $query = $this->db->query("DELETE FROM contact_org WHERE id = '$idField' ");
       return true;
       
   }

   function get_uid() {
        if( ! isset($this->uid)){
            $this->uid = $this->session->userdata('uid');
        }
		return $this->uid;	
   }

	function get_People_id() {
        if( ! isset($this->people_id)){
            $this->set_People_id();
        }
		return $this->people_id;	
    }
    
	function set_People_id() {
        $this->get_uid();
        $query = $this->db->query("SELECT `people_id` FROM `contact_user` WHERE uid = '$this->uid' LIMIT 1");
        if ($query->num_rows() > 0) {
            $row = $query->row();
            $this->people_id = $row->people_id;
        }else{
            return FALSE;
        }
    }
   
   

	function get_Name() {
		return $this->Name;	}

	function set_Name($Name) {
		$this->Name = $Name;	}

	function get_Org_Type() {
		return $this->Org_Type;	}

	function set_Org_Type($Org_Type) {
		$this->Org_Type = $Org_Type;	}

	function get_Street() {
		return $this->Street;	}

	function set_Street($Street) {
		$this->Street = $Street;	}

	function get_Postcode() {
		return $this->Postcode;	}

	function set_Postcode($Postcode) {
		$this->Postcode = $Postcode;	}

	function get_Country() {
		return $this->Country;	}

	function set_Country($Country) {
		$this->Country = $Country;	}

	function get_Phone() {
		return $this->Phone;	}

	function set_Phone($Phone) {
		$this->Phone = $Phone;	}

	function get_FAX() {
		return $this->FAX;	}

	function set_FAX($FAX) {
		$this->FAX = $FAX;	}

	function get_Website() {
		return $this->Website;	}

	function set_Website($Website) {
		$this->Website = $Website;	}

	function get_Notes() {
		return $this->Notes;	}

	function set_Notes($Notes) {
		$this->Notes = $Notes;	}

	function get_First() {
		return $this->first;	}

	function set_First($first) {
		$this->first = $first;	}

	function get_Last() {
		return $this->last;	}

	function set_Last($last) {
		$this->last = $last;	}



      // Function used to initilialise class variables.
      // NOTE: Not particularly useful unless you are using model persistence
      // NOTE: You may want to add default values here.
      function _init_Contact_org()
      {
		$this->id = "";
		$this->people_id = "";
		$this->Name = "";
		$this->Org_Type = "";
		$this->Street = "";
		$this->Postcode = "";
		$this->Country = "";
		$this->Phone = "";
		$this->FAX = "";
		$this->Website = "";
		$this->Notes = "";
		$this->first = "";
		$this->last = "";

      }

      // Initialize all your default variables here
      // Function used to initilialise class variables.
      // NOTE: Not particularly useful unless you are using model persistence
      // NOTE: You could add default values here, but fields are generally set empty
      function _emptyContact_org()
      {
		$this->id = "";
		$this->people_id = "";
		$this->Name = "";
		$this->Org_Type = "";
		$this->Street = "";
		$this->Postcode = "";
		$this->Country = "";
		$this->Phone = "";
		$this->FAX = "";
		$this->Website = "";
		$this->Notes = "";
		$this->first = "";
		$this->last = "";

      }

}

?>